<template>
  <div class="container">
    <div class="iten_content product_main">
      <div class="product_pic"><img v-if="details.project_image" :src="imgUrl+details.project_image"></div>
      <div class="fonts">
        <h3>{{details.project_name}}</h3>
        <h5>{{details.hospital_name}}</h5>
      </div>
    </div>
    <div class="iten_content">
      <div class="item" v-if="details.user">
        <span>顾 客 姓 名：</span>{{details.user.realname}}
      </div>
      <div class="item" v-if="details.user">
        <span>客 户 手 机：</span>{{details.user.username}}
      </div>
      <div class="item" v-if="details.user">
        <span class="w2">年 龄</span>：{{details.user.age}}
      </div>
      <div class="item" v-if="details.user">
        <span class="w2">性 别</span>：{{details.user.sex==0?'保密':(details.user.sex==1?'男':'女')}}
      </div>
    </div>
    <div class="iten_content">
      <div class="item">
        <span>上 级 姓 名：</span>{{details.server_name}}
      </div>
      <div class="item">
        <span>上 级 手 机：</span>{{details.server_phone}}
      </div>
    </div>
    <div class="iten_content">
      <!-- <div class="title">请选择核销卡券<span v-if="details.ticket_array"><span v-if="details.is_check==0">已选</span> {{details.is_check==0?cardId.length:(details.ticket_array.length>0?details.ticket_array.length:0)}}</span></div> -->
      <div class="title">{{details.is_show_button==1?'请选择核销卡券':details.is_show_button==0?'已核销卡券':''}}<span v-if="details.ticket_array"><span v-if="details.is_show_button==1">已选</span> {{details.is_show_button==1?cardId.length:(details.ticket_array.length>0?details.ticket_array.length:'')}}</span></div>
      <div class="card_main">
        <div class="card_list" :class="{'active':item.choose==true}" v-for="(item,index) in details.ticket_array"
          :key="index" @click="item.status!=1?chooseCard(item):''">
          <div class="left">
            <h3><span>[{项目]</span>{{item.name}}</h3>
            <h5>{{item.start_time}}~{{item.end_time}}</h5>
          </div>
          <div class="right" v-if="item.status!=1"></div>
        </div>
      </div>
      <div class="submit" v-if="details.is_show_button==1" @click="submitBtn()">核销</div>
    </div>
  </div>
</template>

<script>
import api from "../../assets/js/api";
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      details: {},
      cardId: [],
      orderId:''
    };
  },
  created() {
    
  },
  components: {},
  mounted() {
    document.title = "卡券核销";
    this.axios.defaults.baseURL = api;
    this.orderId=location.hash.split(":")[1]
    this.getDetails();
  },
  methods: {
    getDetails() {
      let loading = this.loading("加载中", 60000);
      loading.show();
      this.axios
        .post("api/butler/order/card/info", {
          order_id: this.orderId
        })
        .then(msg => {
          loading.hide();
          if (msg.data.code == 200) {
            let arr = msg.data.data;
            if (arr.ticket_array && arr.ticket_array.length > 0) {
              arr.ticket_array.forEach(item => {
                item.choose = false;
              });
            }
            this.details = arr;
          } else if (msg.data.code == 250) {
            Toast({
              message: "登录过期，请重新登陆",
              position: "bottom",
              duration: 2000
            });
            this.$router.push("/login");
            return false;
          } else {
            Toast({
              message: msg.data.msg,
              position: "bottom",
              duration: 2000
            });
          }
        })
        .catch(error => {
          Toast({
            message: "服务器出错啦",
            position: "bottom",
            duration: 2000
          });
        });
    },
    chooseCard(card) {
      let cardId = [];
      this.details.ticket_array.forEach(item => {
        if (item.id == card.id) {
          item.choose = !item.choose;
        }
      });
      this.details.ticket_array.forEach(item => {
        if (item.choose == true) {
          cardId.push(item.id);
        }
      });
      this.cardId = cardId;
    },
    submitBtn() {
      let that = this
      if(this.cardId.length<=0){
        Toast({
              message: "请选择核销卡券",
              position: "bottom",
              duration: 2000
            });
            return
      }
      let loading = this.loading("加载中", 60000);
      loading.show();
      this.axios
        .post("api/butler/order/card/deal", {
          order_id: this.details.id,
          ticket_ids:this.cardId
        })
        .then(msg => {
          loading.hide();
          if (msg.data.code == 200) {
             Toast({
              message: "核销完成",
              position: "bottom",
              duration: 2000
            });
            setTimeout(function(){
               that.$router.push({ path: "/bookingDetail:" + that.details.id });
            },2000)
          } else if (msg.data.code == 250) {
            Toast({
              message: "登录过期，请重新登陆",
              position: "bottom",
              duration: 2000
            });
            this.$router.push("/login");
            return false;
          } else {
            Toast({
              message: msg.data.msg,
              position: "bottom",
              duration: 2000
            });
          }
        })
        .catch(error => {
          Toast({
            message: "服务器出错啦",
            position: "bottom",
            duration: 2000
          });
        });
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  background: rgba(204, 204, 204, 0.2);
}
.iten_content {
  margin-bottom: 10px;
  background: #fff;
  padding: 0 15px;
  box-sizing: border-box;
  &:last-child {
    margin-bottom: 0;
  }
  &.product_main {
    padding: 15px;
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  .product_pic,
  .product_pic img {
    width: 120px;
    height: 120px;
    background: rgba(204, 204, 204, 1);
  }
  .fonts {
    flex: 1;
    text-align: center;
    h3 {
      font-size: 17px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: rgba(102, 102, 102, 1);
      line-height: 24px;
      margin-bottom: 10px;
    }
    h5 {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(102, 102, 102, 1);
      line-height: 20px;
    }
  }
  .item {
    box-sizing: border-box;
    padding: 13px 0;
    border-bottom: 1px solid rgba(204, 204, 204, 0.29);
    font-size: 17px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #888;
    line-height: 24px;
    display: flex;
    span {
      color: #666;
      display: inline-block;
      width: 30%;
      overflow: hidden;
      text-align: left;
      &.w2 {
        letter-spacing: 21px; /*如果需要y个字两端对齐，则为(x-y)/(y-1),这里是（4-2）/(2-1)=2em */
        margin-right: -21px;
      }
    }
    &:last-child {
      border-bottom: none;
    }
  }
  .title {
    box-sizing: border-box;
    padding: 13px 0;
    font-size: 17px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(102, 102, 102, 1);
    line-height: 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span {
      font-size: 17px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(136, 136, 136, 1);
      line-height: 24px;
    }
  }
  .card_list {
    width: 100%;
    padding: 10px 20px;
    border-radius: 10px;
    box-sizing: border-box;
    border: 1px solid rgba(229, 229, 229, 1);
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 10px;
    .left {
      flex: 1;
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      line-height: 18px;
      h3 {
        color: #333;
        overflow: hidden; /*超出部分隐藏*/
        white-space: nowrap; /*不换行*/
        text-overflow: ellipsis; /*超出部分文字以...显示*/
        margin-bottom: 6px;
        span {
          color: rgba(201, 127, 180, 1);
        }
      }
      h5 {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: rgba(102, 102, 102, 1);
        line-height: 16px;
      }
    }
    .right {
      margin-left: 10px;
      width: 14px;
      height: 14px;
      background: url(https://img.yuepinyuemei.com/h5_images/lifebeauty/plusnobuy/3.1.2.1/icon_coupon@2x.png)
        no-repeat;
      background-size: 100%;
      border-radius: 50%;
    }
    &.active .right {
      background: url(https://img.yuepinyuemei.com/h5_images/lifebeauty/plusnobuy/3.1.2.1/icon_coupon_choose@2x.png)
        no-repeat;
      background-size: 100%;
    }
  }
  .submit {
    margin: 20px auto;
    width: 280px;
    height: 48px;
    background: linear-gradient(
      180deg,
      rgba(255, 221, 221, 1) 0%,
      rgba(255, 131, 166, 1) 100%
    );
    border-radius: 24px;
    font-size: 20px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    line-height: 48px;
    text-align: center;
  }
}
</style>


